package com.site_assembly.apiImpl;


import com.service.StorageApiService;
import com.service.form.StorageForm;
import com.site_assembly.common.GetIdUtil;
import com.site_assembly.common.ValidationUtils;
import com.util.Result;
import com.zsd.apiService.StorageApi;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
@Api(tags = "子站点端（仓库管理）接口", description = "提供子站点（仓库管理）相关的 Rest API")
public class StorageApiImpl implements StorageApiService {

    @Resource
    private StorageApi storageApi;

    @Resource
    private GetIdUtil getIdUtil;

    @Override
    @ApiOperation("根据储存废品类型分页查询所有仓库信息")
    @Cacheable(cacheNames = "storage:",key = "#wasteType")
    public Result findAllStorageByPage(@ApiParam(name = "wasteType", value = "存储废品类型") String wasteType,
                                       @ApiParam(name = "current", value = "当前页", example = "1") Integer current,
                                       @ApiParam(name = "pageSize", value = "页容量", example = "1") Integer pageSize) {
        log.info("存储废品类型{}，当前页{},页容量{}，", wasteType, current, pageSize);

        Integer siteId = getIdUtil.getId();

        Result storages = storageApi.findAllStorageByPageAndWasteType(wasteType, siteId, current, pageSize);

        return storages;
    }


    @Override
    @ApiOperation("新增仓库")
    @ApiResponses({@ApiResponse(code = 20000, message = "新增成功"), @ApiResponse(code = 40000, message = "新增失败")})
    public Result addStorage(@ApiParam(name = "storageForm", value = "仓库form") StorageForm storageForm) {

        log.info("新建仓库{}", storageForm);
        //参数校验
        ValidationUtils.validate(storageForm);

        //根据请求头jwt获取站点Id
        Integer siteId = getIdUtil.getId();

        com.zsd.apiService.form.StorageForm form = new com.zsd.apiService.form.StorageForm();
        form.setSiteId(siteId);
        BeanUtils.copyProperties(storageForm, form);
        Result result = storageApi.addStorage(form);

        return result;
    }

    @Override
    @ApiOperation("修改仓库容量")
    @ApiResponses({@ApiResponse(code = 20000, message = "修改成功"), @ApiResponse(code = 40000, message = "修改失败")})
    public Result setStorageCapacityByStorageId(@ApiParam(name = "storageCapacity", value = "以储存容量", example = "1.0") double storageCapacity,
                                                @ApiParam(name = "storageNum", value = "仓库编号") Integer storageId) {

        log.info("以储存容量{},仓库编号{}", storageCapacity, storageId);

        Result result = storageApi.setStorageCapacityByStorageId(storageCapacity, storageId);

        return result;
    }
}
